package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.WYUserDaoI;
import sy.model.SysUser;
import sy.model.WYUser;
@Repository("wyUserDao")
public class WYUserDaoImpl implements WYUserDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(WYUser wyUser) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(wyUser);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from WYUser where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public void modify(WYUser wyUser) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(wyUser);
	}

	@Override
	public WYUser getWYUserById(String id) {
		// TODO Auto-generated method stub
		String hql = "from WYUser where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("id",id);
		List<WYUser> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WYUser> listWYUser() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from WYUser").list();
	}

	@Override
	public WYUser getWYUserByWYAccountId(String wyAccountId) {
		// TODO Auto-generated method stub
		String hql = "from WYUser where wyAccount.id = :wyAccountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("wyAccountId",wyAccountId);
		List<WYUser> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WYUser> listWYUserByAccountId(List<String> accountIds) {
		// TODO Auto-generated method stub
		String hql = "from WYUser where wyAccount.id in :accountIds";
		Query query = getCurrentSession().createQuery(hql);
		query.setParameterList("accountIds", accountIds);
		List<WYUser> list = query.list();
		return list;
	}
}
